package com.lynx.core;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;

import com.lynx.core.tation.ApiPath;
/**
 * 
 * @author lxp
 *
 */
public class XmlPushMap {
	public static Map<String, ApiModel> APIMAPS = new HashMap<String, ApiModel>();

	public void SetMap(Class<?> cls,String postfix) throws ServletException {
		Method[] methods = cls.getDeclaredMethods();
		for (Method method : methods) {
			boolean hasAnnotation = method.isAnnotationPresent(ApiPath.class);
			if (!hasAnnotation) {
				continue;
			}
			ApiPath annotation = (ApiPath) method.getAnnotation(ApiPath.class);
			ApiModel model = new ApiModel();
			model.setClassName(cls.getName());
			model.setMethodName(method.getName());
			model.setModel(annotation.model());
			model.setType(annotation.Type().name());
			if(StringUtils.isNotBlank(annotation.Postfix())){
				model.setPostfix(annotation.Postfix());
			}else{
				model.setPostfix(postfix);
			}
			model.setPattern(annotation.Pattern().name());
			if (APIMAPS.containsKey(annotation.Path())) {
				throw new ServletException("已经存在URL:".concat(annotation.Path()));
			} else {
				APIMAPS.put(annotation.Path(), model);
			}
			System.out.println("ApiPath(method = ".concat(method.getName()).concat(",").concat(annotation.Path()).concat(" )"));
		}
	}
}